package com.dts.controller;

import com.dts.advice.R;
import com.dts.dto.UserDto;
import com.dts.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto) {
        String token = userService.login(userDto.getUsername(), userDto.getPassword());
        if(token == null || token.isEmpty()){
            return R.error("用户名或密码错误");
        }
        return R.data(token);
    }

    @PostMapping("/register")
    public R register(@RequestBody UserDto userDto) {
        userService.register(userDto.getUsername(), userDto.getPassword());
        return R.ok();
    }


}
